#include <bits/stdc++.h>

#define in read()
#define fi first
#define se second
#define pb push_back
#define rep(i, x, y) for(int i = (x); i <= (y); i++)
#define per(i, x, y) for(int i = (x); i >= (y); i--)

using namespace std;

typedef long long ll;
typedef double db;
typedef vector < int > vec;
typedef pair < int , int > pii;

int read() {
    int x = 0; bool f = 0; char ch = getchar(); while(!isdigit(ch)) f |= ch == '-',ch = getchar();
    while(isdigit(ch)) x = x * 10 + (ch ^ 48),ch = getchar(); return f ? -x : x;
}

const int N = 2e5 + 10;

int n;
ll ans;
pii p[N], st1[N], st2[N];
int stk1[N], top1, stk2[N], top2;

void solve(int l, int r) {
	if(l == r) return; int mid = l + r >> 1;
	solve(l, mid); solve(mid + 1, r);
	top1 = top2 = 0; int pos = mid + 1;
	rep(i, l, mid) {
		while(pos <= r && p[pos].se > p[i].se) {
			while(top2 && p[stk2[top2]].fi > p[pos].fi) top2--;
			stk2[++top2] = pos++;
		}
		while(top1 && p[stk1[top1]].fi < p[i].fi) top1--;
		if(top1) {
			int l = 1, r = top2;
			while(l <= r) {
				int mid = l + r >> 1;
				if(p[stk2[mid]].se > p[stk1[top1]].se) l = mid + 1;
				else r = mid - 1;
			}
			ans += top2 - l + 1;
		} else ans += top2;
		stk1[++top1] = i;
	}
	top1 = top2 = 0;
	rep(i, l, mid) st1[++top1] = p[i];
	rep(i, mid + 1, r) st2[++top2] = p[i];
	int pos1 = 1, pos2 = 1;
	rep(i, l, r) {
		if(pos1 <= top1 && (pos2 == top2 + 1 || st1[pos1].se > st2[pos2].se)) p[i] = st1[pos1++];
		else p[i] = st2[pos2++];
	}
}

int main() {
#ifndef ONLINE_JUDGE
    freopen("1.in","r",stdin);
#endif
	n = in; rep(i, 1, n) p[i].fi = in, p[i].se = in;
	sort(p + 1,p + n + 1); solve(1, n); cout << ans << endl; return 0;
}
